package com.woniuxy.rabbitmq.receiver;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.woniuxy.feign.AuctionFeign;
import com.woniuxy.service.RabbitMQService;
import com.work.framework.pojo.ResultDTO;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: wanghao
 * @Date: 2019/9/27 14:54
 */
@Component
public class StartReceiver {

    @Resource
    RabbitMQService rabbitMQService;
    @Resource
    AuctionFeign auctionFeign;
    @RabbitListener(queues = {"delayedStartQueue"}, containerFactory = "rabbitListenerContainerFactory")
    public void handleMessage(Message message, Channel channel) throws Exception {
        // 处理消息
        System.out.println("dealStartQueue {} handleMessage :" + new String(message.getBody()));
        Map<String,Integer> map= (Map) JSONObject.parse(new String(message.getBody()));
        ResultDTO resultDTO = rabbitMQService.startAuction(map.get("gid"));
        ResultDTO resultDTO1 = auctionFeign.cashChange(map.get("aid"));
        if (resultDTO.getCode() == 200 && resultDTO1.getCode()==200) {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } else {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
        }
    }
}
